package com.gxuwz.medical.domain.baoxiao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.gxuwz.medical.dao.ProveDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.policy.Policy;
import com.gxuwz.medical.domain.prove.Prove;

public class BaoXiao {
	private String baoxiaono;
	private String ybcardno;//不保存到数据库
	private String idcard;
	private String illname;
	private String cureno;
	private double curemoney;
	private Date curetime;
	private double premoney;
	private double totalmoney;
	private double relmoney;
	private Date baoxiaotime;
	
	public BaoXiao(){
		
	}
	
	public BaoXiao(String idcard, String illname,
			String cureno, double curemoney, Date curetime, double premoney,
			double totalmoney, double relmoney, Date baoxiaotime) {
		super();
		createBaoxiaono();
		this.idcard = idcard;
		this.illname = illname;
		this.cureno = cureno;
		this.curemoney = curemoney;
		this.curetime = curetime;
		this.premoney = premoney;
		this.totalmoney = totalmoney;
		this.relmoney = relmoney;
		this.baoxiaotime = baoxiaotime;
	}

	public void countmoney() throws SQLException{
		Policy p =new Policy();
		if(p.hasSet){
			premoney=curemoney*p.getRatio();
			totalmoney = getTotalmoney();
			double leftmoney = p.getTopline()-totalmoney;
			relmoney = leftmoney>premoney?premoney:leftmoney;
		}
	}

	private void createBaoxiaono() {
		this.baoxiaono = "MBBX"+String.valueOf(new Date().getTime());
	}

	public double getTotalmoney() throws SQLException {
		double totalmoney=0;
		DbUtil db = new DbUtil();
		Connection conn=db.getConn();
		Statement stmt = conn.createStatement();
		String sql ="select sum(relmoney) from baoxiao where idcard='"+idcard+"'";
		ResultSet rs = db.executeQuery(stmt, sql);
		if(rs.next())
			totalmoney=rs.getDouble(1);
		return totalmoney;
	}
	

	public String checkInfo() throws SQLException{//检查报销信息
		Prove p = new Prove();
		DbUtil db = new DbUtil();
		Connection conn=db.getConn();
		Statement stmt = conn.createStatement();
		String sql ="select * from prove where idcard='"+idcard+"'";
		ResultSet rs = db.executeQuery(stmt, sql);
		if(rs.next()){
			p = new ProveDao().handle(rs);
		}else{
			return Prove.NO_PROVE_ERROR;
		}
		if(p.isExpire())
			return Prove.PROVE_EXPIRE_ERROR;
		if(!this.illname.equals(p.getIllname()))
			return Prove.ILLNAME_NOT_MATCH_ERROR;
		return "";	
	}
	
	public String getBaoxiaono() {
		return baoxiaono;
	}
	public void setBaoxiaono(String baoxiaono) {
		this.baoxiaono = baoxiaono;
	}
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	public String getIllname() {
		return illname;
	}
	public void setIllname(String illname) {
		this.illname = illname;
	}
	public String getCureno() {
		return cureno;
	}
	public void setCureno(String cureno) {
		this.cureno = cureno;
	}
	public double getCuremoney() {
		return curemoney;
	}
	public void setCuremoney(double curemoney) {
		this.curemoney = curemoney;
	}
	public Date getCuretime() {
		return curetime;
	}
	public void setCuretime(Date curetime) {
		this.curetime = curetime;
	}
	public double getPremoney() {
		return premoney;
	}
	public void setPremoney(double premoney) {
		this.premoney = premoney;
	}
	public double getRelmoney() {
		return relmoney;
	}
	public void setRelmoney(double relmoney) {
		this.relmoney = relmoney;
	}
	public Date getBaoxiaotime() {
		return baoxiaotime;
	}
	public void setBaoxiaotime(Date baoxiaotime) {
		this.baoxiaotime = baoxiaotime;
	}
	public void setTotalmoney(double totalmoney) {
		this.totalmoney = totalmoney;
	}
	public String getYbcardno() {
		return ybcardno;
	}

	public void setYbcardno(String ybcardno) {
		this.ybcardno = ybcardno;
	}

	public static void main(String[]args) throws ParseException, SQLException{

	}
	
}
